package com.babyqiao.wallpaper.manager.service.impl;


import com.babyqiao.wallpaper.manager.controller.dto.AddAlbumParams;
import com.babyqiao.wallpaper.manager.dao.AlbumMapper;
import com.babyqiao.wallpaper.manager.entity.AlbumTagRel;
import com.babyqiao.wallpaper.manager.entity.WallpaperAlbum;
import com.babyqiao.wallpaper.manager.controller.dto.AlbumQuery;
import com.babyqiao.wallpaper.manager.entity.vo.ActorResVO;
import com.babyqiao.wallpaper.manager.entity.vo.AlbumResVO;
import com.babyqiao.wallpaper.manager.service.AlbumService;
import com.babyqiao.wallpaper.manager.service.IAlbumTagRelService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @author qinglongzhai
 */
@Service
public class AlbumServiceImpl extends ServiceImpl<AlbumMapper, WallpaperAlbum> implements AlbumService {
    @Autowired
    private IAlbumTagRelService albumTagRelService;
    /**
     * 构建查询条件方法
     */
    @Override
    public Page<AlbumResVO> addSelectCondition(Page<AlbumResVO> page, AlbumQuery albumQuery) {

        //        //构建查询条件
//        QueryWrapper<WallpaperAlbum> queryWrapper = new QueryWrapper<>();
//
//        String id = albumQuery.getId();
//
//        String name = albumQuery.getName();
//
//        String online = albumQuery.getOnline();
//
//        String startTime = albumQuery.getStartTime();
//
//        String endTime = albumQuery.getEndTime();
//
//        if (id != null) {
//            queryWrapper.eq("id", id);
//        }
//        if (name != null) {
//            queryWrapper.like("album_name", name);
//        }
//
//        if (online != null) {
//            queryWrapper.eq("online", online);
//        }
//        if (startTime != null) {
//            queryWrapper.ge("gmt_modified", startTime);
//        }
//        if (endTime != null) {
//            queryWrapper.le("gmt_modified", endTime);
//        }
//        //排序
//        queryWrapper.orderByDesc("gmt_modified");
        return baseMapper.findAndPage(page, albumQuery);
    }

    /**
     * @param id
     * @param state : 上下线状态
     * @Author qinglongZhai
     * @Date 2020/12/10
     * * @param id: 专辑ID
     */
    @Override
    @Caching(evict = {
            @CacheEvict(value = "HomeContent",key = "'HomeContent'"),
            @CacheEvict(value = "AlbumContent",allEntries=true)
            })
    public UpdateWrapper<WallpaperAlbum> addUpdataCondition(String id, Integer state) {
        UpdateWrapper<WallpaperAlbum> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.set("online", state);
        return updateWrapper;
    }

    /**
     * 添加专辑
     *
     * @param addAlbumParams : 添加专辑的参数
     * @Author QLZ
     * @Date 2021/2/20
     * @return: boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @Caching(evict = {
            @CacheEvict(value = "HomeContent",key = "'HomeContent'"),
            @CacheEvict(value = "AlbumContent",allEntries=true)
    })
    public boolean addAblumService(AddAlbumParams addAlbumParams) {
        WallpaperAlbum wallpaperAlbum=new WallpaperAlbum();


        BeanUtils.copyProperties(addAlbumParams,wallpaperAlbum);
        boolean save = this.save(wallpaperAlbum);
       // System.out.println("save的值"+save);


        for(String item:addAlbumParams.getTagId()){
            AlbumTagRel albumTagRel=new AlbumTagRel();
            albumTagRel.setAlbumId(wallpaperAlbum.getId());
            albumTagRel.setTagId(item);
            boolean b = albumTagRelService.save(albumTagRel);
            //System.out.println("b的值"+save);
        }

        return true;
    }

    /**
     * 修改专辑
     *
     * @param addAlbumParams :修改专辑的参数
     * @Author QLZ
     * @Date 2021/2/20
     * @return: boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @Caching(evict = {
            @CacheEvict(value = "HomeContent",key = "'HomeContent'"),
            @CacheEvict(value = "AlbumContent",allEntries=true)
    })
    public boolean updateAlbumService(AddAlbumParams addAlbumParams) {
        WallpaperAlbum wallpaperAlbum=new WallpaperAlbum();


        //更新专辑信息
        BeanUtils.copyProperties(addAlbumParams,wallpaperAlbum);
        boolean update = this.updateById(wallpaperAlbum);

        //先删除所有关系
        QueryWrapper<AlbumTagRel> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("album_id",wallpaperAlbum.getId());
        boolean remove = albumTagRelService.remove(queryWrapper);

        //再重新添加关系
        for(String item:addAlbumParams.getTagId()){
            AlbumTagRel albumTagRel=new AlbumTagRel();
            albumTagRel.setAlbumId(wallpaperAlbum.getId());
            albumTagRel.setTagId(item);
            boolean b = albumTagRelService.save(albumTagRel);
            //System.out.println("b的值"+save);
        }

        return true;
    }

    /**
     * 获得全部专辑
     *
     * @Author QLZ
     * @Date 2021/2/20
     * @return: java.util.List<com.babyqiao.wallpaper.manager.entity.vo.AlbumResVO>
     */
    @Override
    public List<AlbumResVO> getAllAlbumService() {
        List<AlbumResVO> albumResVOS = baseMapper.getAllAlbum();
        return albumResVOS;
    }

    /**
     * 根据id获取专辑
     *
     * @Author QLZ
     * @Date 2021/2/20
     * @return: com.babyqiao.wallpaper.manager.entity.vo.AlbumResVO
     */
    @Override
    public AlbumResVO getAlbumbyIdService(String id) {
        AlbumResVO albumResVO=baseMapper.getAlbumById(id);
        return albumResVO;
    }

    /**
     * 删除专辑同时删除标签关联表中的关系
     *
     * @param id : 专辑id
     * @Author QLZ
     * @Date 2021/2/21
     * @return: boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @Caching(evict = {
            @CacheEvict(value = "HomeContent",key = "'HomeContent'"),
            @CacheEvict(value = "AlbumContent",allEntries=true)
    })
    public boolean delAlbumService(String id) {

        QueryWrapper<AlbumTagRel> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("album_id",id);
        albumTagRelService.remove(queryWrapper);
        int i = baseMapper.deleteById(id);
        //System.out.println(i);
        return true;
    }
}
